#include "VideoPlayerUi.h"
#include "ui_VideoPlayerUi.h"
#include "WidgetHandler.h"
#include "BootSetting.h"
#include "MiscData.h"
#include "LogoUtility.h"
#ifdef arm
#include "Mpp.h"
#endif
#include "Mcu.h"

#include <AuthorityDialog.h>

VideoPlayerUi::VideoPlayerUi(QWidget *parent) :
    BaseWidget(parent),
    ui(new Ui::VideoPlayerUi)
{
    ui->setupUi(this);
    ui->left->setVisible(false);
    ui->right->setVisible(false);
    LogoUtility::getInstance()->addLogoLabel(ui->logo);
#ifdef arm
    QObject::connect(Mpp::getInstance(), &Mpp::syncMp4VideoPositionSignal, this, [this](QString position){
        QString duration = Mpp::getInstance()->getMp4VideoDuration();
        ui->position->setText(QString("%1/%2").arg(position).arg(duration));
    });
#endif
}

VideoPlayerUi::~VideoPlayerUi()
{
    delete ui;
}

void VideoPlayerUi::activate()
{
#ifdef arm
    Mpp::getInstance()->stopRealtimeImage();
#endif

    QRect rect(0, 0, 1280, 800);
    QString path = RuntimeData::MiscData::getInstance()->g_miscDataInfo.currentMp4Path;
    qDebug()<<"mp4:path"<<path;
#ifdef arm
    Mpp::getInstance()->startMp4VideoPlayer(rect, path);
#endif
}

void VideoPlayerUi::inactivate()
{
#ifdef arm
    Mpp::getInstance()->stopMp4VideoPlayer();
    Mpp::getInstance()->startRealtimeImage();
#endif
}

void VideoPlayerUi::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;

    default:
        break;
    }
}

bool VideoPlayerUi::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == ui->video && event->type() == QEvent::MouseButtonPress) {
        RuntimeData::MiscData::getInstance()->g_miscDataInfo.fullScreenEntryType = 2;
        hide();/* 全屏显示 */
    }
}
void VideoPlayerUi::on_gohome_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();
    WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
}

void VideoPlayerUi::on_goback_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();
    WidgetHandler::getInstance()->displayWidget(PatientInfoUi_Id);
}

void VideoPlayerUi::on_pushButton_clicked() {

    int userId = RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.currentUserId;

    if (userId < 0) {
        AuthorityDialog::getInstance()->setParent(this);
        AuthorityDialog::getInstance()->setAuthorityType(AuthorityType_Null);
        AuthorityDialog::getInstance()->exec();
        return;
    } else {
        Mpp::getInstance()->stopMp4VideoPlayer();
        QString str = RuntimeData::MiscData::getInstance()->g_miscDataInfo.currentMp4Path;
        QFile(str).remove();
        str.chop(3);
        QFile(str+="jpg").remove();
        Mcu::getInstance()->touchSoundBeepMessage();
        WidgetHandler::getInstance()->displayWidget(PatientInfoUi_Id);
    }
}
